<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: ad_registry_loader.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * This is the admin loader for the Fusion Registry
 *
 * @package Fusion Registry
 * @subpackage admin
 * @version $Id: ad_registry_loader.php 4 2008-08-03 01:43:03Z registry.fusion $
 */

if ( ! defined( 'AD_REGISTRY_CALLED' ) )
{
    print "<h1>Incorrect access</h1>You cannot access this file directly.";
    exit();
}

/**
 * Class ad_registry_loader
 *
 * @package Fusion Registry
 * @subpackage admin
 */
class ad_registry_loader
{
    var $ipsclass;

    /**#@+ @var string */
    var $base_url;
    var $r_version_short = 'v3.0.4';
    var $r_version_long = '30004';
    /**#@-*/

    /** @var object */
    var $class = '';

    /** @var array Used as a shortcut lookup for the images used in the various admin interfaces */
    var $imgs = array();

    /**
     * Intialization of ad_registry_loader
     *
     * @param object $class Parent class object
     */
    function auto_run($class)
    {
        $this->class = &$class;

        // unset globals
        $tmp_in = array_merge( $_GET, $_POST, $_COOKIE );

        foreach ( $tmp_in as $k => $v )
        {
            unset($$k);
        }

        // Installer present?
		if ( @file_exists(REGISTRY_PATH.'install/index.php') )
		{
			if ( !@file_exists(REGISTRY_PATH.'install/install.lock') )
			{
				$this->ipsclass->html .= $this->warning_unlocked_instagrade('install');
			}
			else
			{
				$this->ipsclass->html .= $this->warning_instagrade('install');
			}
		}

        // Upgrader present?
		if ( @file_exists(REGISTRY_PATH.'upgrade/index.php') )
		{
			if ( !@file_exists(REGISTRY_PATH.'upgrade/upgrade.lock') )
			{
				$this->ipsclass->html .= $this->warning_unlocked_instagrade('upgrade');
			}
			else
			{
				$this->ipsclass->html .= $this->warning_instagrade('upgrade');
			}
		}

        // Setup some img shortcuts
        $this->imgs = array( 'edit'         => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/edit.png" alt="Edit" title="Edit" border="0" />',
                             'delete'       => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/delete.png" alt="Delete" title="Delete" border="0" />',
                             'folder_arrow' => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/browse.png" alt="Open" title="Open" border="0" />',
                             'warning'      => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/warning.png" alt="WARNING!" title="WARNING!" border="0" />',
                             'home'         => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/home.png" alt="Home" title="Home" border="0" />',
                             'search'       => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/search.png" alt="Search" title="Search" border="0" />',
                             'star'         => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/star.png" alt="Star" title="Star" border="0" />',
                             'ok'           => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/ok.png" alt="Ok" title="Ok" border="0" />',
                             'add'          => '<img src="../style_images/'. $this->ipsclass->skin['_imagedir'] .'/registry_images/add.png" alt="Add" title="Add" border="0" />',
                           );

        // Grab the config
        $this->ipsclass->DB->query("SELECT config_name,config_value FROM ibf_registry_config");
        while ( $row = $this->ipsclass->DB->fetch_row() )
        {
            $this->class->registry_config[ $row['config_name'] ] = $row['config_value'];
        }

        // Do we have a version mismatch?
        if ( $this->class->registry_config['version_long'] != $this->r_version_long )
        {
            $this->ipsclass->html .= $this->version_mismatch();
        }

        return true;
    }

    /**
     * Update/insert config data
     *
     * @param array Config data to save in form 'config_name' => 'config_value'
     * @return boolean TRUE|FALSE Returns FALSE upon failure or invalid incoming array structure
     */
    function update_config( $config_list = array() )
    {
        if ( is_array($config_list) and count($config_list) > 0 )
        {
            // Loop through and update/insert config options
            foreach ( $config_list as $config_name => $config_value )
            {
                // First see if we already have an existing config or if we need to insert a new one
                $this->ipsclass->DB->query("SELECT config_name FROM ibf_registry_config WHERE config_name = '$config_name'");

                if ( $this->ipsclass->DB->get_num_rows() > 0 )
                {
                    // Create the db update string
                    $db_string = $this->ipsclass->DB->compile_db_update_string( array( 'config_value' => $config_value ) );

                    $this->ipsclass->DB->query("UPDATE ibf_registry_config SET $db_string WHERE config_name='$config_name'");
                }
                else
                {
                    $db_string = $this->ipsclass->DB->compile_db_insert_string( array( 'config_name' => $config_name,
                                                                                       'config_value' => $config_value,
                                                                              )      );

                    $this->ipsclass->DB->query("INSERT INTO ibf_registry_config (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
                }
            }

            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }


    /**
     * Builds the html to warn users about an unlocked installer/upgrader 'instagrade' still present
     *
     * @param string Specify the type, either 'install' or 'upgrade'
     * @return string Raw html to display wrapped in a <div> ... </div>
     */
    function warning_unlocked_instagrade($type)
    {
        $title = ($type == 'install') ? 'Installer' : 'Upgrader';

        $html = "<div style='color:red;border:1px solid red;background:#FFC0C3;padding:10px'>".
                "<span style='font-size:20px;font-weight:bold'>Warning: Unlocked $title Still Present</span>".
                "<br /><br /><span style='font-size:14px;'>Remove the <b>".REGISTRY_PATH."$type/</b> directory from your server at once!".
                "<br />Leaving it on your server WILL compromise the security of your system.</span></div><br />";

        return $html;
    }


    /**
     * Builds the html to warn users of a version mismatch
     *
     * @return string Raw html to display warning about version mismatch
     */
    function version_mismatch()
    {
        $html = "<div style='color:red;border:1px solid red;background:#FFC0C3;padding:10px'>".
                "<span style='font-size:20px;font-weight:bold'>Warning: Version Mismatch Detected!</span>".
                "<br /><br /><span style='font-size:14px;'>The version number of the code running '". $this->r_version_short .
                "' (". $this->r_version_long .") does not match the version number in the database '". $this->class->registry_config['version_short'] ."' (".
                $this->class->registry_config['version_long'] .") ! If you recently upgraded make sure you uploaded all the files ".
                "and that you have completed the upgrade script.</span></div><br />";

        return $html;
    }


    /**
     * Builds the html to warn users about a locked installer/upgrader 'instagrade' still present
     *
     * @param string Specify the type, either 'install' or 'upgrade'
     * @return string Raw html to display wrapped in a <div> ... </div>
     */
    function warning_instagrade($type)
    {
        $title = ($type == 'install') ? 'Installer' : 'Upgrader';

        $html = "<div style='color:red;border:1px solid red;background:#FFE1E2;padding:10px'>".
                "<span style='font-size:14px;font-weight:bold'>Warning: $title Still Present</span>".
                "<br /><br /><span style='font-size:10px;'>Although the $title appears to be locked, we recommend you remove it ".
                "from your server for security.<br />".
                "Simply remove the directory <b>".REGISTRY_PATH."$type/</b> from your server to remove this message.</span></div><br />";

        return $html;
    }

}
?>